package ai.people.core.nebula.tx;

import ai.people.netmon.utils.StringPool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhenglin
 * @date 2022/6/25
 */
public class NebulaTransactionUtils {
    private static final Pattern VERTEX_COMPILE = Pattern.compile("(?i)INSERT VERTEX");
    private static final Pattern VALUE_COMPILE = Pattern.compile("(?i)VALUES ([\"'].+[\"']):");
    private static final Pattern EDGE_COMPILE = Pattern.compile("(?i)INSERT EDGE");
    private static final Pattern EDGE_VALUE_COMPILE = Pattern.compile("(?i)EDGE (.+)\\(.*\\) VALUES ([\"'].+[\"']):");

    public static String getRollbackGql(String gql) {
        //INSERT VERTEX t2(name, age) VALUES "12":("n1", "a13");  DELETE VERTEX "team1";
        //INSERT EDGE e2(name, age) VALUES "11"->"13":("n1", 12); DELETE EDGE serve "player100" -> "team204";
        String[] split = gql.split(StringPool.SEMICOLON);
        StringBuilder sb = new StringBuilder();
        for (String s : split) {
            String match = getMatch(s);
            if (match != null) {
                sb.append(match);
            }
        }
        if (sb.length() > 0) {
            return sb.toString();
        }
        return null;
    }

    private static String getMatch(String gql) {
        Matcher matcher = VERTEX_COMPILE.matcher(gql);
        if (matcher.find()) {
            Matcher matcher1 = VALUE_COMPILE.matcher(gql);
            if (matcher1.find()) {
                String group = matcher1.group(1);
                return "DELETE VERTEX " + group + ";";
            }
        }
        matcher = EDGE_COMPILE.matcher(gql);
        if (matcher.find()) {
            Matcher matcher1 = EDGE_VALUE_COMPILE.matcher(gql);
            if (matcher1.find()) {
                String group = matcher1.group(1);
                String group2 = matcher1.group(2);
                return "DELETE EDGE " + group + " " + group2 + ";";
            }
        }
        return null;
    }

    public static void main(String[] args) {
        {
            String gql = "INSERT VERTEX t2(name, age) VALUES 'facebook:28hff':(\"n1\", \"a13\");";
            System.out.println(getRollbackGql(gql));
        }
        {
            String gql = "INSERT EDGE e2(name, age) VALUES \"11\"->\"13\":(\"n1\", 12);";
            System.out.println(getRollbackGql(gql));
        }
        {
            String gql = "INSERT EDGE accomplice() VALUES \"gang_members:1xHEVz6trHl\"->\"gang_members:1xHEVz6trHm\":()";
            System.out.println(getRollbackGql(gql));
        }
        {
            String gql = "INSERT EDGE accomplice() VALUES \"gang_members:1xHEVz6trHl\"->\"gang_members:1xHEVz6trHm\":();INSERT EDGE accomplice() VALUES \"gang_members:1xHEVz6trHl\"->\"gang_members:1xHEVz6trHm\":();INSERT EDGE accomplice() VALUES \"gang_members:1xHEVz6trHl\"->\"gang_members:1xHEVz6trHm\":();";
            System.out.println(getRollbackGql(gql));
        }
        {
            String gql = "INSERT VERTEX t2(name, age) VALUES 'facebook:28hff':(\"n1\", \"a13\");INSERT VERTEX t2(name, age) VALUES 'facebook:28hff':(\"n1\", \"a13\");INSERT VERTEX t2(name, age) VALUES 'facebook:28hff':(\"n1\", \"a13\");";
            System.out.println(getRollbackGql(gql));
        }
    }
}
